package com.samuel.basics.controller.admin;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.samuel.basics.annotation.CacheLock;
import com.samuel.basics.annotation.OperateLog;
import com.samuel.basics.base.BaseController;
import com.samuel.basics.constant.CommonConstant;
import com.samuel.basics.constant.OperateConstant;
import com.samuel.basics.entity.OperateLogEntity;
import com.samuel.basics.enums.ResultEnum;
import com.samuel.basics.exception.CustomException;
import com.samuel.basics.service.OperateLogService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Arrays;


/**
* <p>
* 操作日志-前端控制器
* </p>
*
* @author 黄阿能
* @since 2020-01-20
*/

@Controller
@RequestMapping("/operateLog")
public class OperateLogController extends BaseController {
    
    private static final Logger logger = LoggerFactory.getLogger(OperateLogController.class);
    
    @Resource
    private OperateLogService operateLogService;

    private final static String PREFIX = "operateLog/";

    /**
    * 跳转到操作日志首页
    */
    @GetMapping("/list")
    public String list() {
        return PREFIX + LIST;
    }

    @ResponseBody
    @GetMapping("/listJson")
    // @OperateLog(operateModule = "操作日志管理", operateType = OperateConstant.SELECT, operateDesc = "查看操作日志列表")
    public JSONObject listJson(Page<OperateLogEntity> iPage, OperateLogEntity operateLogEntity) {
        QueryWrapper<OperateLogEntity> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(operateLogEntity.getType())) {
            queryWrapper.lambda().eq(OperateLogEntity::getType, operateLogEntity.getType());
        }
        if (StringUtils.isNotBlank(operateLogEntity.getMethod())) {
            queryWrapper.lambda().like(OperateLogEntity::getMethod, operateLogEntity.getMethod());
        }
        if (StringUtils.isNotBlank(operateLogEntity.getModule())) {
            queryWrapper.lambda().like(OperateLogEntity::getModule, operateLogEntity.getModule());
        }
        if (StringUtils.isNotBlank(operateLogEntity.getUserName())) {
            queryWrapper.lambda().like(OperateLogEntity::getUserName, operateLogEntity.getUserName());
        }
        if (StringUtils.isNotBlank(operateLogEntity.getIp())) {
            queryWrapper.lambda().like(OperateLogEntity::getIp, operateLogEntity.getIp().trim());
        }
        queryWrapper.lambda().orderByDesc(OperateLogEntity::getCreateDate);
        queryWrapper.select(OperateLogEntity.class, info-> !StringUtils.equals(info.getColumn(), "response_params"));
        iPage = operateLogService.page(iPage, queryWrapper);
        return page2LayUiTablePage(iPage);
    }

    @GetMapping("/view")
    @CacheLock(prefix = "view")
    @OperateLog(operateModule = "操作日志管理", operateType = OperateConstant.VIEW, operateDesc = "查看操作日志详情")
    public String view(String id, ModelMap modelMap) {
        if (StringUtils.isBlank(id)) {
            throw new CustomException(ResultEnum.GET_ERROR, "id不存在");
        }
        OperateLogEntity operateLogModel = operateLogService.getById(id);
        modelMap.put("operateLogModel", operateLogModel);
        return PREFIX + VIEW;
    }

    /**
    * 批量删除
    *
    * @param ids id集合
    */
    @ResponseBody
    @PostMapping("delete")
    @CacheLock(prefix = "operateLog-delete")
    @OperateLog(operateModule = "操作日志管理", operateType = OperateConstant.DELETE, operateDesc = "删除操作日志，可以批量删除")
    public JSONObject delete(String ids) {
        JSONObject jsonObject = new JSONObject();
        if (StringUtils.isEmpty(ids)) {
            jsonObject.put(CommonConstant.AjaxCode.CODE, CommonConstant.AjaxCode.FAIL_CODE);
            jsonObject.put(CommonConstant.AjaxCode.MESSAGE, "请至少选择一条数据！");
            return jsonObject;
        }
        try {
            operateLogService.removeByIds(Arrays.asList(ids.split(CommonConstant.DOC_FLAG)));
            jsonObject.put(CommonConstant.AjaxCode.CODE, CommonConstant.AjaxCode.SUCCESS_CODE);
            jsonObject.put(CommonConstant.AjaxCode.MESSAGE, "删除操作日志数据成功！");
        } catch (Exception e) {
            logger.error("删除失败！原因： {}", e.getMessage());
            jsonObject.put(CommonConstant.AjaxCode.CODE, CommonConstant.AjaxCode.FAIL_CODE);
            jsonObject.put(CommonConstant.AjaxCode.MESSAGE, "删除操作日志数据失败！");
        }

        return jsonObject;
    }

}
